package com.zr.serviceimpl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zr.dao.UpdateUserDao;
import com.zr.model.Clog;
import com.zr.model.Crole;
import com.zr.model.Cuser;
import com.zr.service.UpdateUserService;

@Service("updateUserService")
public class UpdateUserServiceImpl implements UpdateUserService{
	@Resource
	private UpdateUserDao updateUserDao;
	
	@Override
	public int getChangedOrNot(String rname,String ustate,int uid) {
		Crole crole = updateUserDao.getRoleByRname(rname);
		Cuser u = updateUserDao.getUserByUid(uid);
		
		if(rname.equals(u.getCrole().getRname())&&ustate.equals(u.getUstate())){
			return 0;
		}else{
			return 1;
		}
	}

	@Override
	public void updateUserByInfo(int uid, String ustate,String uname,int cuid,String cuname,String rname) {
		Crole crole = updateUserDao.getRoleByRname(rname);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String time = sdf.format(date);
		Cuser u = updateUserDao.getUserByUid(cuid);
		if (ustate==u.getUstate()) {
			StringBuffer infos = new StringBuffer();
			infos.append("编号为");
			infos.append(cuid);
			infos.append("的用户");
			infos.append(cuname);
			infos.append("在");
			infos.append(time);
			infos.append("把ID为");
			infos.append(uid);
			infos.append("的用户");
			infos.append(uname);
			infos.append("权限改为");
			infos.append(crole.getRname());
			String info = infos.toString();
			updateUserDao.updateUserByInfo(uid, ustate, crole);
			updateUserDao.insertInfoIntoLog(time, cuname, cuid, info);
		} else if(rname==u.getCrole().getRname()) {
			StringBuffer infos = new StringBuffer();
			infos.append("编号为");
			infos.append(cuid);
			infos.append("的用户");
			infos.append(cuname);
			infos.append("在");
			infos.append(time);
			infos.append("把ID为");
			infos.append(uid);
			infos.append("的用户");
			infos.append(uname);
			infos.append("的状态改为");
			infos.append(ustate);
			String info = infos.toString();
			updateUserDao.updateUserByInfo(uid, ustate, crole);
			updateUserDao.insertInfoIntoLog(time, uname, uid, info);
		}else{
			StringBuffer infos = new StringBuffer();
			infos.append("编号为");
			infos.append(cuid);
			infos.append("的用户");
			infos.append(cuname);
			infos.append("在");
			infos.append(time);
			infos.append("把ID为");
			infos.append(uid);
			infos.append("的用户");
			infos.append(uname);
			infos.append("权限改为");
			infos.append(crole.getRname());
			infos.append(",并把用户状态改为");
			infos.append(ustate);
			String info = infos.toString();
			updateUserDao.updateUserByInfo(uid, ustate, crole);
			updateUserDao.insertInfoIntoLog(time, uname, uid, info);
		}
	}

	
}
